package com.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.beans.AdminInfo;
import com.beans.MenuInfo;
import com.beans.Roleinfo;
import com.common.Common;
import com.dao.AdminDao;
import com.dao.MenuDao;
import com.dao.RoleDao;
import com.utils.PageInfo;
import com.utils.PageUtil;

/**
 * Servlet 角色管理
 */
@WebServlet("/RoleServlet.do")
public class RoleServlet extends HttpServlet {
	private RoleDao _roleDao = new RoleDao();
	private AdminDao _adminDao = new AdminDao();
	private MenuDao _menuDao=new MenuDao();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setCharacterEncoding("utf-8"); // 重点在于生成什么格式编码的数据
		response.setContentType("text/html; charset=UTF-8");
		String flag = request.getParameter("flag");
		if ("manage".equals(flag)) {
			manage(request, response);
		} else if ("listadmin".equals(flag)) {
			listadmin(request, response);
		} else if ("adminchange".equals(flag)) {
			adminchange(request, response);
		} else if ("adminUpdate".equals(flag)) {
			adminUpdate(request, response);
		}else if ("delete".equals(flag)) {
			delete(request, response);
		}else if ("roleForUpdate".equals(flag)) {
			roleForUpdate(request, response);
		}else if ("roleUpdate".equals(flag)) {
			roleUpdate(request, response);
		} else if ("searchForUpdate".equals(flag)) {
			searchForUpdate(request, response);
		} else if ("update".equals(flag)) {
			update(request, response);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	/**
	 * 角色管理
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void manage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Roleinfo> roleList = _roleDao.getAllrole();
		request.setAttribute("roleList", roleList);
		request.getRequestDispatcher("/perm/role_manage.jsp").forward(request, response);
	}

	/**
	 * 用户角色管理
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void listadmin(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int pageSize = Common.PAGE_SIZE;
		int rowCount = _adminDao.getAdminCount();
		int pageIndex = 1;
		String pageIndexStr = request.getParameter("pageIndex");
		if (pageIndexStr != null) {
			pageIndex = Integer.parseInt(pageIndexStr);
		}
		PageInfo page = PageUtil.getPageInfo(pageSize, rowCount, pageIndex);
		List<AdminInfo> adminList = _adminDao.getAdminList(page);
		request.setAttribute("adminList", adminList);
		request.setAttribute("page", page);
		request.getRequestDispatcher("/perm/role_listadmin.jsp").forward(request, response);
	}

	/**
	 * 角色用户修改跳转
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void adminchange(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id");
		int idnumber = Integer.parseInt(id);
		AdminInfo admin = _adminDao.getAdminInfo(idnumber);
		request.setAttribute("admin", admin);
		List<Roleinfo> role = _roleDao.getAllrole();
		request.setAttribute("role", role);
		request.getRequestDispatcher("/perm/role_adminchange.jsp").forward(request, response);
	}

	/**
	 * 角色用户修改
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void adminUpdate(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id");
		int idnumber = Integer.parseInt(id);
		String ck_id = request.getParameter("ck_id");
		int ck_idnumber = Integer.parseInt(ck_id);
		int result = _adminDao.updateRoleId(idnumber, ck_idnumber);
		if (result == 1) {
			response.getWriter().print("用户角色信息更新成功");
		}else {
			response.getWriter().print("用户角色信息更新失败");
		}
	}
	
	/**
	 * 删除角色
	 */
	private void delete(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id=request.getParameter("id");
		 int idnumber=Integer.parseInt(id);
		 int rest=_roleDao.delRole(idnumber);
		 if(rest==1) {
			 manage(request, response);
		 }
	}
	
	/**
	 * 角色权限绑定跳转
	 */
	private void roleForUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		int idnumber = Integer.parseInt(id);
		List<Integer> ints=_menuDao.getRoleLists(idnumber);
		Roleinfo role=_roleDao.getRole(idnumber);
		request.setAttribute("ints", ints);
		request.setAttribute("role", role);
		request.getRequestDispatcher("/perm/role_rolechange.jsp").forward(request, response);
	}
	
	/**
	 * 角色权限绑定修改
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	private void roleUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		int idnumber = Integer.parseInt(id);
		String[] ck_ids=request.getParameterValues("ck_id");
		int rest=_menuDao.updateRoleMenu(idnumber, ck_ids);
		if(rest>0) {
			request.setAttribute("msg", "修改成功");
			roleForUpdate(request, response);
		}
	}
	
	/*
	 * 角色修改跳转
	 */
	private void searchForUpdate(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		int idnumber = Integer.parseInt(id);
		Roleinfo role=_roleDao.getRole(idnumber);
		request.setAttribute("role", role);
		request.getRequestDispatcher("/perm/role_change.jsp").forward(request, response);
	}
	
	/*
	 * 角色修改
	 */
	private void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		String roleName = request.getParameter("roleName");
		String des = request.getParameter("des");
		int idnumber = Integer.parseInt(id);
		Roleinfo roleinfo=new Roleinfo();
		roleinfo.setId(idnumber);
		roleinfo.setDes(des);
		roleinfo.setRoleName(roleName);
		int result =_roleDao.updateRole(roleinfo);
		if (result == 1) {
			response.getWriter().print("角色更新成功");
		}else {
			response.getWriter().print("角色更新失败");
		}
		
	}
}
